package com.dji.compose_learning.todolist

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import com.dji.compose_learning.ui.view.TodoListPage
import kotlin.random.Random

class TodoListActivity : ComponentActivity() {
    companion object {
        @JvmStatic
        val Titles = listOf("上语文课", "上数学课", "上英语课", "午休", "课间操", "课间休息", "上活动课", "晚自习", "打游戏娱乐")
    }

    private val viewModel by viewModels<TodoListViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            TodoListPage(addItem = {
//                viewModel.todolist.postValue(mutableListOf()) //清除有效

                //新增无效
//                val list = viewModel.todolist.value
//                list?.add(TodoItem(Titles[Random.Default.nextInt(0, Titles.size)],TodoIcon.Event))
//                viewModel.todolist.postValue(list)

                //有效，这里的区别，viewmodel的todolist需要重新更新 不能在原有的list中添加数据 否则不刷新
//                val list = mutableListOf<TodoItem>()
//                list.addAll(viewModel.todolist.value ?: listOf())
//                list.add(TodoItem(Titles[Random.Default.nextInt(0, Titles.size)], TodoIcon.Event))
//                viewModel.todolist.postValue(list)

                viewModel.addItem(TodoItem(Titles[Random.Default.nextInt(0,
                    Titles.size)], TodoIcon.Event))
            })
//            , editItem = { item ->
            //无效移除
//                viewModel.todolist.value?.remove(item)

//                val list = mutableListOf<TodoItem>()
//                list.addAll(viewModel.todolist.value?: listOf())
//                list.remove(item)
//                viewModel.todolist.postValue(list)

//                viewModel.removeItem(item)
//        }
        }
    }
}